Skip to main content

企业微信端内登陆和扫码登陆 前后分离的方案

步骤

  1. 页面调用接口获取二维码页面的地址,并传入回调的网址。
  2. 企业微信二维码页面扫码后,跳转到步骤1的回调地址并附上了code。
  3. 回调页面获取code请求获取token地址的接口。
  4. 得到token保存在本地,over。

路由

$api = app('Dingo\Api\Routing\Router');

//这里的version是版本,里面的v1是在env里面定义好的。
$api->version('v1', function($api) {

//不需要jwt认证的接口
$api->group(['middleware' => 'log'] , function () use ($api) {

/**
* 登陆认证模块
*/
//导购端
$api->group(['prefix' => 'guide','namespace' => 'App\Http\Controllers\V1\Guide'], function ($api) {
$api->any('/login', "AuthorizeController@login");
$api->get('/qr_login', "AuthorizeController@qrLogin"); //扫码登陆
$api->any('/get_token', "AuthorizeController@getToken");
});
//销售端
$api->group(['prefix' => 'sell','namespace' => 'App\Http\Controllers\V1\Sell'], function ($api) {
$api->any('/login', "AuthorizeController@login");
$api->get('/qr_login', "AuthorizeController@qrLogin"); //扫码登陆
$api->any('/get_token', "AuthorizeController@getToken");
});
//后台端
$api->group(['prefix' => 'admin','namespace' => 'App\Http\Controllers\V1\Admin'], function ($api) {
$api->any('/login', "AuthorizeController@login");
$api->get('/qr_login', "AuthorizeController@qrLogin"); //扫码登陆
$api->any('/get_token', "AuthorizeController@getToken");
});


####################

});

});

认证的代码

<?php
/**
* Created by PhpStorm.
* User: 清行
* Date: 2020/3/6
* Time: 14:44
*/

namespace App\Http\Controllers\V1\OAuth;
use App\Common\Err\ApiErrDesc;
use App\Exceptions\ApiException;
use App\Services\JwtServices;
use Illuminate\Http\Request;

class AuthorizeController extends OAuthController
{
// public function __construct()
// {
// $this->api = $this->wework(__NAMESPACE__);
// }
//config('wework.CORP_ID')
//企业微信内登陆
public function login(Request $request)
{
$redirect_uri = $request->input('redirect_uri');
if (empty($redirect_uri)) {
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->corpid . '&redirect_uri=' . $this->redirect_uri . '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
}else{
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->corpid . '&redirect_uri=' . $redirect_uri. '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
}
return $this->jsonSuccessData($url);
}

//扫码登陆
public function qrLogin(Request $request)
{
$redirect_uri = $request->input('redirect_uri');
if (empty($redirect_uri))
{
$url = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=".$this->corpid."&agentid=".$this->agentid."&redirect_uri=".$this->redirect_uri."&state=STATE";
}else{
$url = "https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=".$this->corpid."&agentid=".$this->agentid."&redirect_uri=".$redirect_uri."&state=STATE";
}

return $this->jsonSuccessData($url);

// $a = "<a href='".$url."'>扫码登陆$url</a>";
// $header = ['Content-Type'=>'text/html'];
// return response()->make($a, 200, $header);
}


/**
* 1. 用code换取用户信息,会得到用户的userid
* 2. 生成jwt
* @param Request $request
* @return false|string
*/
public function getToken(Request $request)
{
$code = $request->input('code');
$userinfo = $this->api->GetUserInfoByCode($code);

if (empty($userinfo->UserId))
{
throw new ApiException(ApiErrDesc::ERR_LOGIN_FAIL);
}else{
$user_info = JwtServices::getUserInfoByDB($userinfo->UserId);
//Web json Token 获取
$jwt = JwtServices::generateJwt($user_info);
$data = [
'userid' => $userinfo->UserId,
'token' => $jwt,
];
}
return $this->jsonSuccessData($data);
}

}

企业微信端内登陆

1. 步骤1的页面

会自动跳转到二维码页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>

http://yfdg.tyunai.cn/login.html

</body>

<script>
$.get("https://work.tyunai.cn/api/guide/login",{redirect_uri:"http://yfdg.tyunai.cn/code.html"},function(data){
console.log(data)
window.location.replace(data.data);
});
</script>
</html>

2.企业微信跳转到回调uri并附带code

使用code获取jwt

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用code获取JWT</title>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>

</body>
<script>
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
var code = getUrlParam('code');
$.get("https://work.tyunai.cn/api/guide/get_token",{code:code}, function(data){
console.log(data);
});

</script>
</html>

扫码登陆

1. 步骤1的页面

会自动跳转到二维码页面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>

http://yfht.tyunai.cn/login.html

</body>

<script>
$.get("https://work.tyunai.cn/api/admin/qr_login",{redirect_uri:"http://yfht.tyunai.cn/code.html"},function(data){
console.log(data)
window.location.replace(data.data);
});
</script>
</html>

2. 扫码后回调页面

得到code掉用接口获取json web token

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用code获取JWT</title>
<script src="jquery-1.9.1.min.js"></script>
</head>
<body>

</body>
<script>
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
var code = getUrlParam('code');
$.get("https://work.tyunai.cn/api/admin/get_token",{code:code}, function(data){
console.log(data);
});

</script>
</html>